package com.demo.daemon;

/**
 * @author gy
 * @date 2022/4/23
 */
public class Thread04 {
    public static void main(String[] args) throws InterruptedException {
        MyDaemonThread myDaemonThread = new MyDaemonThread();
        Thread thread1 = new Thread(myDaemonThread);
        // 将子线程设置为守护线程
        thread1.setDaemon(true);
        // 启动子线程
        thread1.start();

        for (int i = 0; i < 100; i++) {
            System.out.println("main 线程在工作");
            Thread.sleep(50);
        }
        System.out.println("主线程工作结束了");
    }
}
class MyDaemonThread implements Runnable{

    @Override
    public void run() {
        while (true){
            System.out.println("子线程在一直陪着主线程在工作");
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
